package com.power.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.power.constant.ManagerConstants;
import com.power.domain.SysMenu;
import com.power.mapper.SysMenuMapper;
import com.power.service.SysMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.nio.file.AccessDeniedException;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
@CacheConfig(cacheNames = "com.power.service.impl.SysMenuServiceImpl")
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements SysMenuService{
@Autowired
private  SysMenuMapper sysMenuMapper;

    /**
     * 根据用户标识查询菜单权限集合
     * @param userId
     * @return
     */
    @Override
    @Cacheable(key = "#userId")
    public Set<SysMenu> getMenuListById(Long userId) {
      Set<SysMenu> perms =  sysMenuMapper.selectListmenuById(userId);
      Set<SysMenu> sysMenuS=toTreeMenuSet(perms,0l);
        return sysMenuS;
    }

    /**
     * 查询所有菜单权限
     * @return
     */
    @Override
    @Cacheable(key = ManagerConstants.GETSYS_ALL_MENU_KEY)
    public List<SysMenu> queryAllSysMenuList() {
        List<SysMenu> sysMenus = sysMenuMapper.selectList(null);
        return sysMenus;
    }

    @Override
    @CacheEvict(key = ManagerConstants.GETSYS_ALL_MENU_KEY)
    public boolean saveOne(SysMenu sysMenu) {
        return sysMenuMapper.insert(sysMenu)>0;
    }

    @Override
    public boolean updateMenuOne(SysMenu sysMenu) {
        if (sysMenu==null)
        {
         new AccessDeniedException("参数不能为空");
        }
        if (0==sysMenu.getType())
        {
            sysMenu.setParentId(0l);
        }
        return sysMenuMapper.updateById(sysMenu)>0;
    }

    @Override
    @CacheEvict(key = ManagerConstants.GETSYS_ALL_MENU_KEY)
    public boolean deleteById(Long id) {
        //删除目录之前要先判断该目录下面是否包含子目录
        List<SysMenu> sysMenus = sysMenuMapper.selectList(new LambdaQueryWrapper<SysMenu>().eq(SysMenu::getParentId, id));
        if (!StringUtils.isEmpty(sysMenus)&&(sysMenus.size()>0))
        {
            //集合不为空，说明下面有子目录
            new Exception("该目录下有子目录，不能删除");
            return false;
        }else {
            //集合为空的话进行删除
            return sysMenuMapper.deleteById(id) > 0;
        }
    }


    private Set<SysMenu> toTreeMenuSet(Set<SysMenu> perms, long l) {
        Set<SysMenu> roots = perms.stream()
                .filter(s -> s.getParentId().equals(l))
                .collect(Collectors.toSet());
        roots.forEach(root->{
            Set<SysMenu> Children = perms.stream().filter(s -> s.getParentId().equals(root.getMenuId()))
                    .collect(Collectors.toSet());
            root.setList(Children);
        });
//        Set<SysMenu> roots = perms.stream()
//                    .filter(s -> s.getParentId().equals(l))
//                    .collect(Collectors.toSet());
//        roots.forEach(root->root.setList(toTreeMenuSet(perms,root.getMenuId())));
        return roots;

    }
}
